#include "Player.h"

//check to see if player sprite is present
bool Player::initSprites(void)
{
	spriteSurface = dxPMgr->getSurfaceFromBitmap("./mc.bmp", 276, 78);
	if (spriteSurface == NULL)
		return false;
	/*if (enemySurface == NULL)
		return false;*/
	for (int i = 0; i < MAX_SPRITES; i++)
	{
		srcRect.top = 0;
		srcRect.left = i * 40;
		srcRect.right = srcRect.right + 40;//spriteStruct[i].srcRect.left + 40;
		srcRect.bottom = srcRect.top + 40;//40;

		
		posX = float(rand()%600);
		posY = float(rand()%430);

		// set the animation data
		curFrame = 0;
		numFrames = 4;

		// set the move data
		moveX = 0.0;//40.0;
		moveY = 0.0;	
	}
	return true;
}

//get input from player
int Player::getInput(int c)
{
	cas = c;
	return cas;
}

//draw player sprite
void Player::drawSprite()
{
	//input(c);
	//int cas = input;
	float anim_rate= 0;
	// set the source rect to the correct frame position
	srcRect.top = 0;
	srcRect.bottom = srcRect.top + 78;
	srcRect.left = curFrame * 69;
	srcRect.right = srcRect.left + 69;
	
	// move the sprite by it's movement rate
	posX += moveX * (anim_rate * 6);
	posY += moveY * (anim_rate * 6);
	
	// make sure that this sprite is not going off the screen 
	//with the upcoming if statements

	if (posX > MAX_LOCX)
	{
		posX = MAX_LOCX;
	}

	if (posX < 0)
	{
		posX = 0.0;
	}

	if (posY > MAX_LOCY)
	{
		posY = MAX_LOCY;
	}

	if (posY < 40)
	{
		posY = 40.0;
	}

    //the next set of if statements makes it so that it changes 
    //what sprite is being displayed depending on the key press
	//after the key is pressed cas is reset to a different number
	//to stop the sprite from moving and retaining its direction
	if (cas == 0) //w
	{
		if (posY <= 0)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 0; //sets the current sprite animation frame
		}
		else
		{
			moveX = 0.0;//40.0;
			moveY = -120.0;
			curFrame = 0;	
			cas = 5;
		}

	}
	else if (cas == 1) //s
	{
		
		if (posY >= MAX_LOCY)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 1;
		}
		else
		{
			moveX = 0.0;//40.0;
			moveY = 120.0;
			curFrame = 1;
			cas = 6;
		}
	}
	else if (cas == 2) //a
	{
		if (posX <= 0)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 2;
		}
		else
		{
			moveX = -120.0;//40.0;
			moveY = 0.0;
			curFrame = 2;
			cas = 7;
		}
	}
	else if (cas == 3) //d
	{
		if (posX >= MAX_LOCX)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 3;
		}
		else
		{
			moveX = 120.0;// 40.0;
			moveY = 0.0;
			curFrame = 3;
			cas = 8;
		}
	}

///////////////////////////////////////////////////////////////////////////////
//series of checks to stop the character from moving and fixing his direction.
//////////////////////////////////////////////////////////////////////////////

else if (cas == 5) //after cas moves the sprite in a direction, stop it and retain the sprite image
	{
		if (posX >= MAX_LOCX)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 0;
		}
		else
		{
			moveX = 0.0;// 40.0;
			moveY = 0.0;
			curFrame = 0;
		}
	}

	else if (cas == 6) //after s is hit
	{
		if (posX >= MAX_LOCX)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 1;
		}
		else
		{
			moveX = 0.0;// 40.0;
			moveY = 0.0;
			curFrame = 1;
		}
	}

else if (cas == 7) //after a is hit
	{
		if (posX >= MAX_LOCX)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 2;
		}
		else
		{
			moveX = 0.0;// 40.0;
			moveY = 0.0;
			curFrame = 2;
		}
	}

	else if (cas == 8) //after d is hit
	{
		if (posX >= MAX_LOCX)
		{
			moveX = 0.0;//40.0;
			moveY = 0.0;
			curFrame = 3;
		}
		else
		{
			moveX = 0.0;// 40.0;
			moveY = 0.0;
			curFrame = 3;
		}
	}


	// set the destination for the player sprite
	RECT destRect;
	destRect.left = int(posX);
	destRect.top = int(posY);
	destRect.bottom = destRect.top + PLAYER_HEIGHT;
	destRect.right = destRect.left + PLAYER_WIDTH;

	// draw player sprite to back buffer
	dxPMgr->blitToSurface(spriteSurface, &srcRect, &destRect);
}